import { post } from 'lib/net'
import * as api from 'lib/constants/api'

let wxSign = async (callBack) => {
  let res = await post(api.getSignature, {url: window.location.href.split('#')[0]})
  if (res.data) {
    let data = res.data
    window.wx.config({
      debug: false,
      appId: data.appId,
      timestamp: data.timestamp,
      nonceStr: data.nonceStr,
      signature: data.signature,
      jsApiList: ['hideMenuItems', 'scanQRCode']
    })
    window.wx.ready(() => {
      window.wx.hideMenuItems({
        menuList: ['menuItem:share:appMessage',
          'menuItem:share:timeline',
          'menuItem:share:qq',
          'menuItem:share:weiboApp',
          'menuItem:favorite',
          'menuItem:share:facebook',
          'menuItem:share:QZone'
        ]
      })
    })
    if (callBack) {
      callBack({success: true})
    }
  } else {
    if (callBack) {
      callBack({success: false, msg: '网络异常请重试'})
    }
  }
}

module.exports = {
  wxSign
}
